<?php

/**
 * Created by PhpStorm.
 * User: liuxiu
 * Date: 2022/7/15
 * Time: 09:59
 */

namespace search\controllers\bigdata;


use business\elasticsearch\services\BigDataPurchaseService;

class PurchaseController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/purchase/get-list 01、大数据商机搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataPurchase
     *
     * @apiDescription 大数据商机搜索
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} province_code             省份
     * @apiParam {String} city_code                 城市
     * @apiParam {String} start_time                发布开始时间
     * @apiParam {String} end_time                  发布结束时间
     * @apiParam {String} company_type              企业类型
     * @apiParam {String} source_type               信息来源
     * @apiParam {String} page                      页码
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.organizing_code      社会统一信用码
     * @apiSuccess {String}  data.list.developer_name       采购商公司名
     * @apiSuccess {String}  data.list.developer_id         平台开发商id
     * @apiSuccess {String}  data.list.uid                  源数据id
     * @apiSuccess {String}  data.list.title                标题
     * @apiSuccess {String}  data.list.publish_time         发布时间
     * @apiSuccess {String}  data.list.end_time             报名截止时间
     * @apiSuccess {String}  data.list.detail               采购详情
     * @apiSuccess {String}  data.list.ower                 招标负责人
     * @apiSuccess {String}  data.list.ower_mobile          负责人联系电话
     * @apiSuccess {String}  data.list.agent_organization   招标代理机构
     * @apiSuccess {String}  data.list.agent_ower           代理负责人
     * @apiSuccess {String}  data.list.agent_ower_mobile    代理负责人联系电话
     * @apiSuccess {String}  data.list.url                  源URL
     * @apiSuccess {String}  data.list.data_source          数据来源：平台公开招标、平台招募、官网招标
     * @apiSuccess {String}  data.list.titleHL              高亮标题
     *
     * @apiSuccessExample {json} 成功响应：
     * {"success":true,"message":null,"errorCode":null,"data":{"resultNum":2,"resultSet":[{"pulsar_time":"2022-07-11 20:41:12",
     * "organizing_code":"91430100758035602Y","uid":"83423","developer_name":"大汉控股集团有限公司","title":"怀化大汉龙城智能锁采购安装合同",
     * "publish_time":"2019-05-31 14:07:44","end_time":"2019-06-04 15:48:06","project_city":"智能家居","detail":"","ower":"邓香蕊","ower_mobile":"",
     * "agent_organization":"","agent_ower":"","agent_ower_mobile":"","url":"","data_source":"平台公开招标","data_source_code":"ycg_bid",
     * "nameSuggest":"怀化大汉龙城智能锁采购安装合同",
     * "titleHL":"怀化<em class=\"_high\">大汉</em><em class=\"_high\">龙</em><em class=\"_high\">城</em>智能锁采购安装合同"}]}}
     *
     */
    public function actionGetList()
    {
        try {
            $keyword = $this->getParam('keyword');
            $province = $this->getParam('province_code');
            $city = $this->getParam('city_code');
            $startTime = $this->getParam('start_time');
            $endTime = $this->getParam('end_time');
            $sourceType = $this->getParam('source_type');
            $companyType = $this->getParam('company_type');
            $regStatus = $this->getParam('reg_status', 'get', 0);
            $amountLower = $this->getParam('amount_lower');
            $amountUpper = $this->getParam('amount_upper');
            $phoneType = $this->getParam('phone_type');
            $sortType = $this->getParam('sort_type');
            $areaCodes = $this->getParam('area') ? explode(',', trim($this->getParam('area'))) : [];
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $publishTime = array();
            if (!empty($startTime)) {
                $publishTime['start_time'] = $startTime;
            }
            if (!empty($endTime)) {
                $publishTime['end_time'] = $endTime;
            }
            if (!empty($province)) {
                $province = explode(",", $province);
            } else {
                $province = [];
            }
            if (!empty($city)) {
                $city = explode(",", $city);
            } else {
                $city = [];
            }

            $tags = [];
            $tags['developer_top_100_label'] = $this->getParam('developer_top_100_label');
            $tags['new_decorate_expand_label'] = $this->getParam('new_decorate_expand_label');
            $tags['new_land_usage_expand_label'] = $this->getParam('new_land_usage_expand_label');
            $tags['new_area_expand_label'] = $this->getParam('new_area_expand_label');
            $tags['price_gt_avg_label'] = $this->getParam('price_gt_avg_label');

            $bigDataDeveloperService = new BigDataPurchaseService();
            $rs = $bigDataDeveloperService->getDataList(
                $keyword,
                $province,
                $city,
                $publishTime,
                $sourceType,
                $companyType,
                $regStatus,
                $amountLower,
                $amountUpper,
                $phoneType,
                $sortType,
                $areaCodes,
                $tags,
                $page,
                $pageSize
            );
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/purchase/get-list 03、商机详情
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataPurchase
     *
     * @apiDescription 商机详情
     *
     * @apiParam {String} id                   id
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Number}  data.organizing_code      社会统一信用码
     * @apiSuccess {String}  data.developer_name       采购商公司名
     * @apiSuccess {String}  data.developer_id         平台开发商id
     * @apiSuccess {String}  data.uid                  源数据id
     * @apiSuccess {String}  data.title                标题
     * @apiSuccess {String}  data.publish_time         发布时间
     * @apiSuccess {String}  data.end_time             报名截止时间
     * @apiSuccess {String}  data.detail               采购详情
     * @apiSuccess {String}  data.ower                 招标负责人
     * @apiSuccess {String}  data.ower_mobile          负责人联系电话
     * @apiSuccess {String}  data.agent_organization   招标代理机构
     * @apiSuccess {String}  data.agent_ower           代理负责人
     * @apiSuccess {String}  data.agent_ower_mobile    代理负责人联系电话
     * @apiSuccess {String}  data.url                  源URL
     * @apiSuccess {String}  data.data_source          数据来源：平台公开招标、平台招募、官网招标
     * @apiSuccess {Object}  data.projects              项目列表
     * @apiSuccess {Object}  data.projects.province     项目所在省
     * @apiSuccess {Object}  data.projects.city         项目所在市
     * @apiSuccess {Object}  data.projects.province_code    项目所在省code
     * @apiSuccess {Object}  data.projects.city_code        项目所在城市code
     * @apiSuccess {Object}  data.projects.project_name     项目名称
     *
     * @apiSuccessExample {json} 成功响应：
     * {"success":true,"message":null,"errorCode":null,"data":{"ower":"戴璐","ower_mobile":"15151823825","company_type":"港澳台",
     * "bidding_service_category":"广告","projects":[{"province":"广东省","city":"广州市","city_code":"223",
     * "project_name":"雅居乐地产置业有限公司","province_code":"222"}],"data_source_code":"ycg_bid","organizing_code":"91440101776910853G",
     * "developer_name":"雅居乐地产置业有限公司","end_time":"2018-10-14 23:59:00",
     * "title":"雅居乐山湖城项目2018年11月-2019年10月广告企划及微信运营相关事项招标","agent_ower":"","url":"https://zb.mycaigou.com/bidding-77222.html",
     * "data_source":"平台公开招标","uid":"77222","agent_ower_mobile":"","pulsar_time":"2022-07-27 11:14:52","publish_time":"2018-10-11 14:50:48",
     * "id":"ycg_bid_77222","detail":"","agent_organization":"","nameSuggest":"雅居乐山湖城项目2018年11月-2019年10月广告企划及微信运营相关事项招标"}}
     *
     */
    public function actionGetDetail()
    {
        try {
            $id = $this->getParam('id');
            $bigDataDeveloperService = new BigDataPurchaseService();
            $data = $bigDataDeveloperService->getDetail($id);
            return $this->success($data);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/purchase/guess-names 02、采购商机名联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 采购商机名联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮商机名
     * @apiSuccess {String}  data.list.title       商机名
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $bigDataPurchaseService = new BigDataPurchaseService();
            $rs = $bigDataPurchaseService->guessNames($keyword, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }


    /**
     *
     * @api {post} /bigdata/purchase/subscription 04、商机订阅
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataDeveloper
     *
     * @apiDescription 采购商机订阅
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} area                      分页size
     * @apiParam {String} source                    商机来源
     * @apiParam {String} company_type              企业类型
     * @apiParam {String} purchase_amount_lower     采购预算Lower
     * @apiParam {String} purchase_amount_upper     采购预算Upper
     * @apiParam {String} contact_type              联系方式类型
     * @apiParam {String} createdTime               时间
     * @apiParam {String} page                      页码
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     */
    public function actionSubscription()
    {
        try {
            $keywordStr = $this->getParam('keyword');
            $areaStr = $this->getParam('area');
            $sourceStr = $this->getParam('source');
            $companyTypeStr = $this->getParam('company_type');
            $purchaseAmountLower = $this->getParam('purchase_amount_lower');
            $purchaseAmountUpper = $this->getParam('purchase_amount_upper');
            $contactTypeStr = $this->getParam('contact_type');
            $startTime = $this->getParam('start_time');
            $endTime = $this->getParam('end_time');
            $createdTime = $this->getParam('created_time');
            $needOrgCode = $this->getParam('need_org_code');
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 100;

            $keyword = array_filter(explode(",", $keywordStr));
            $area = array_filter(explode(",", $areaStr));
            $source = array_filter(explode(",", $sourceStr));
            $companyType = array_filter(explode(",", $companyTypeStr));
            $contactType = array_filter(explode(",", $contactTypeStr));

            $publishTime = array();
            if (!empty($startTime)) {
                $publishTime['start_time'] = $startTime;
            }
            if (!empty($endTime)) {
                $publishTime['end_time'] = $endTime;
            }

            $bigDataPurchaseService = new BigDataPurchaseService();
            $rs = $bigDataPurchaseService->subscript(
                $keyword,
                $area,
                $source,
                $companyType,
                $purchaseAmountLower,
                $purchaseAmountUpper,
                $contactType,
                $publishTime,
                $createdTime,
                $needOrgCode,
                $page,
                $pageSize
            );
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }
}
